<template>
  <div>
    <g-dynamic-table
      type="languages"
      ref="table"
      :fetch="fetchDataList"
      :isPage="false"
      :showActionBtn="showActionBtn"
      @action-click="handleActionClick"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'isDisabled'">
          <CheckCircleTwoTone v-if="!record.isDisabled" twoToneColor="#52c41a" />
          <CloseCircleTwoTone v-if="record.isDisabled" twoToneColor="#ff0000" /> </template
      ></template> </g-dynamic-table></div
></template>
<script lang="ts">
  import { defineComponent } from 'vue';
  import { AppComponentBase } from '/@/shared/component-base';
  import GDynamicTable from '/@/shared/components/g-dynamic-table/index.vue';
  import { EVENT_BTN_ENUM } from '/@/shared/components/g-dynamic-table/enums/event-btn-enum';
  import CreateOrEditLanguage from './create-or-edit-languages/create-or-edit-languages.vue';
  import { CheckCircleTwoTone, CloseCircleTwoTone } from '@ant-design/icons-vue';
  import _ from 'lodash';
  import {
    LanguageListDto,
    SetDefaultLanguageInput,
    LanguageServiceProxy,
  } from '/@/shared/service-proxies/service-proxies';
  export default defineComponent({
    name: 'languages-news',
    components: {
      GDynamicTable,
      CreateOrEditLanguage,
      CheckCircleTwoTone,
      CloseCircleTwoTone,
    },
    mixins: [AppComponentBase],
    data() {
      return {
        languageServiceProxy: new LanguageServiceProxy(),
        tenantId: new Number(),
        defaultLanguageName: '',
        selectItems: [] as any[],
      };
    },
    computed: {
      showActionBtn() {
        /**
         * item 按钮json配置项
         * column table 当前列配置
         * record 当前行数据
         */
        return (item, column, record) => {
          switch (item.name) {
            case EVENT_BTN_ENUM.EDIT:
              return !this.isDefaultLanguage(record);
            case EVENT_BTN_ENUM.DELETE:
              return !this.isDefaultLanguage(record);
            default:
              return true;
          }
        };
      },
    },
    mounted() {
      (this.$refs.table as any).setRowSelection('getCheckboxProps', (record) => {
        return {
          disabled: this.isDefaultLanguage(record),
        };
      });
    },
    methods: {
      /** 是否默认多语言 */
      isDefaultLanguage(item: LanguageListDto) {
        return (
          item.displayName === '英语' ||
          item.displayName === '中文' ||
          item.icon === 'famfamfam-flags us' ||
          item.icon === 'famfamfam-flags cn'
        );
      },
      /**
       * 按钮回调事件
       */
      handleActionClick(event: string, item?: any) {
        switch (event) {
          case EVENT_BTN_ENUM.CREATE:
            this.create();
            break;
          case EVENT_BTN_ENUM.DELETE:
            this.deleteItem(item);
            break;
          case EVENT_BTN_ENUM.BULK_EDIT:
            this.bulkEdit();
            break;
          case EVENT_BTN_ENUM.EDIT:
            this.create(item);
            break;
          case EVENT_BTN_ENUM.BULK_DELETE:
            this.batchDelete();
            break;
          case EVENT_BTN_ENUM.CHANGETEXTS:
            this.changeTexts(item);
            break;
          case EVENT_BTN_ENUM.SET_AS_DEFAULT_LANGUAGE:
            this.setAsDefaultLanguage(item);
            break;
          default:
            break;
        }
      },
      //获取数据
      fetchDataList() {
        return new Promise((resolve, reject) => {
          this.languageServiceProxy
            .getLanguages()
            .finally(() => {
              // finishedCallback!();
            })
            .then((res) => {
              resolve(res.items);
              // this.showPaging(res);
            })
            .catch((error) => {
              reject(error);
            });
        });
      },
      /**
       * 创建编辑语言
       */
      create(item?: LanguageListDto) {
        this.modalHelper
          .create(CreateOrEditLanguage, { pageDataList: [item?.id] })
          .subscribe((res) => {
            if (res) {
              (this.$refs.table as any).reloadGoFirstPage();
            }
          });
      },
      /**
       * 批量编辑
       */
      bulkEdit() {
        this.selectItems = [];
        let arr = (this.$refs.table as any).getSelectRowKeys();
        if (arr.length < 1) {
          return this.notify.warn(this.l('PleaseSelectAtLeastOneItem'));
        }
        this.selectItems = arr;
        this.modalHelper
          .create(CreateOrEditLanguage, { pageDataList: this.selectItems })
          .subscribe((res) => {
            if (res) {
              (this.$refs.table as any).reloadGoFirstPage();
            }
          });
      },
      /**
       * 批量删除
       */
      batchDelete() {
        const table = this.$refs.table as any;
        if (table.getSelectRowKeys().length <= 0) {
          this.notify.warn(this.l('PleaseSelectAtLeastOneItem'));
          return;
        }
        const ids: number[] = _.map(table.selectedRowKeys);

        this.message.confirm(
          this.l('ConfirmDeleteXItemsWarningMessage', table.getSelectRowKeys().length),
          '',
          (res: any) => {
            if (res) {
              (this.$refs.table as any).setLoading(true);
              this.languageServiceProxy
                .batchDelete(ids)
                .finally(() => {
                  (this.$refs.table as any).setLoading(false);
                })
                .then(() => {
                  (this.$refs.table as any).reloadGoFirstPage();
                  this.notify.success(this.l('SuccessfullyDeleted'));
                });
            }
          },
        );
      },
      /**
       * 单个删除
       */
      deleteItem(item: LanguageListDto) {
        (this.$refs.table as any).setLoading(true);
        this.languageServiceProxy
          .deleteLanguage(item.id)
          .finally(() => {
            (this.$refs.table as any).setLoading(false);
          })
          .then(() => {
            (this.$refs.table as any).reloadGoFirstPage();
            this.notify.success(this.l('SuccessfullyDeleted'));
          });
      },
      /**
       * 设为默认语言
       */
      setAsDefaultLanguage(item: LanguageListDto) {
        (this.$refs.table as any).setLoading(true);
        let setdefault: SetDefaultLanguageInput = new SetDefaultLanguageInput();
        setdefault.name = item.name as any;
        this.languageServiceProxy
          .setDefaultLanguage(setdefault)
          .finally(() => {
            (this.$refs.table as any).setLoading(false);
          })
          .then(() => {
            (this.$refs.table as any).reloadGoFirstPage();
            this.notify.success(this.l('SettingSuccessfully'));
          });
      },
      /**
       * 更换文本
       */
      changeTexts(item: LanguageListDto) {
        setTimeout(() => {
          this.$router.push({
            path: '/system2/language-texts-news',
            query: { lang: item.name },
          });
        }, 300);
      },
    },
  });
</script>
